package com.dub.firstapp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dub.iterface.ISessionHandle;
import com.dub.model.SecRpcModel;
import com.dub.utils.Session;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ShareActivity extends Activity implements ISessionHandle {
    List<String> commands=new ArrayList<String>();
    ListView listView=null;
    List<SecRpcModel.ModelSecRpcAdvert> lst_strs=new ArrayList<SecRpcModel.ModelSecRpcAdvert>();
    ShareAdapter itemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);

        //添加处理的命令
        commands.add("2-1");

        TextView txt_name=(TextView)findViewById(R.id.share_txt_name);
        txt_name.setText(Session.GetSession().GetAgent().getModelSecRpcAgent().getLoginName());

        listView=(ListView)findViewById(R.id.share_ltv_list);
        itemAdapter=new ShareAdapter(ShareActivity.this,R.layout.share_item,lst_strs);
        listView.setAdapter(itemAdapter);
        //子线程更改ui界面
//        listView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                ShareAdapter itemAdapter=new ShareAdapter(ShareActivity.this,R.layout.share_item,lst_strs);
//                listView.setAdapter(itemAdapter);
//            }
//        },10);

        for (int i=0;i<4;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SecRpcModel.ModelSecRpcAdvertReqFind mdl=SecRpcModel.ModelSecRpcAdvertReqFind.newBuilder()
                            .setStartRow(0)
                            .setPageSize(15)
                            .build();
                    byte[] data=mdl.toByteArray();
                    //添加处理函数与发送命令
                    Session.GetSession().AddHand("ShareActivity",ShareActivity.this);
                    Session.GetSession().Send((short)2,(short)1,data);
                }
            }).start();
        }
    }

    @Override
    public List<String> GetHandCmd() {
        return commands;
    }

    @Override
    public void HandAction(String cmd, byte[] data) {
        try{
            SecRpcModel.ModelSecRpcAdvertResFind mdl=SecRpcModel.ModelSecRpcAdvertResFind.newBuilder().mergeFrom(data).build();
            if (mdl.getErr()>0){
                Toast.makeText(ShareActivity.this,"网络链接错误",Toast.LENGTH_SHORT).show();
                return;
            }
            if (mdl.getRowsCount()<1){
                Toast.makeText(ShareActivity.this,"无分享数据",Toast.LENGTH_SHORT).show();
                return;
            }

            lst_strs.addAll(mdl.getRowsList());
            itemAdapter.notifyDataSetChanged();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
